package com.neusoft.neuiotms.dcam.workflow.converter;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcam.workflow.dto.ArrivalCountDTO;
import com.neusoft.neuiotms.dcam.workflow.dto.ArrivalMainDTO;
import com.neusoft.neuiotms.dcam.workflow.model.ArrivalAsset;
import com.neusoft.neuiotms.dcam.workflow.model.ArrivalCount;
import com.neusoft.neuiotms.dcam.workflow.model.ArrivalMain;
import com.neusoft.neuiotms.dcam.workflow.repository.ArrivalAssetRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.ArrivalCountRepository;
import com.neusoft.neuiotms.dcam.workflow.repository.ArrivalMainRepository;

@Component
public class ArrivalMainConverter extends AbstractConverter<ArrivalMain, ArrivalMainDTO> {

    @Autowired
    private ArrivalMainRepository arrivalMainRepository;
    @Autowired
    private ArrivalCountConverter arrivalCountConverter;
    @Autowired
    private ArrivalCountRepository arrivalCountRepository;
    @Autowired
    private ArrivalAssetRepository arrivalAssetRepository;
    @Autowired
    private ArrivalAssetConverter arrivalAssetConverter;

    @Override
    public ArrivalMain toModel(final ArrivalMainDTO dto) {
        final ArrivalMain model;
        if (dto.isNew()) {
            model = new ArrivalMain();
        } else {
            model = this.arrivalMainRepository.getOne(dto.getId());
        }

        final List<ArrivalCount> list = model.getArrivalCount().stream().collect(Collectors.toList());
        //删除到货验收数量信息
        list.forEach(k -> {
            model.getArrivalCount().remove(k);
            this.arrivalCountRepository.delete(k);
        });

        //到货验收数量
        dto.getArrivalCount().stream().map(k -> {
            final ArrivalCount arrivalModel = this.arrivalCountConverter.toModel(k);
            arrivalModel.setArrivalMain(model);
            return arrivalModel;
        }).filter(s -> null != s).forEach(model.getArrivalCount()::add);

        final List<ArrivalAsset> assetList = model.getAssetList().stream().collect(Collectors.toList());
        assetList.forEach(k -> {
            model.getAssetList().remove(k);
            this.arrivalAssetRepository.delete(k);
        });

        dto.getAssetList().stream().map(k -> {
            final ArrivalAsset assetModel = this.arrivalAssetConverter.toModel(k);
            assetModel.setRuku(model);
            return assetModel;
        }).filter(s -> null != s).forEach(model.getAssetList()::add);

        //合同
        model.setContractCode(dto.getContractCode());
        model.setContractName(dto.getContractName());
        model.setTitle(dto.getTitle());
        model.setRemark(dto.getRemark());
        model.setReceiveDate(dto.getReceiveDate());
        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setTaskId(dto.getTaskId());
        model.setCreateUser(dto.getCreateUser());
        model.setCreateUserName(dto.getCreateUserName());
        model.setComprehensive(dto.getComprehensive());
        model.setPurchaseDate(dto.getPurchaseDate());
        model.setCategories(dto.getCategories());
        model.setLocation(dto.getLocation());
        model.setLocationName(dto.getLocationName());

        return model;
    }

    @Override
    public ArrivalMainDTO toDTO(final ArrivalMain model, final boolean forListView) {
        final ArrivalMainDTO dto = new ArrivalMainDTO();

        dto.setContractCode(model.getContractCode());
        dto.setContractName(model.getContractName());

        dto.setId(model.getId());
        dto.setReceiveDate(model.getReceiveDate());
        dto.setRemark(model.getRemark());
        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setTaskId(model.getTaskId());
        dto.setTitle(model.getTitle());
        dto.setCreateUser(model.getCreateUser());
        dto.setCreateUserName(model.getCreateUserName());
        dto.setComprehensive(model.getComprehensive());
        dto.setPurchaseDate(model.getPurchaseDate());
        dto.setCategories(model.getCategories());
        dto.setLocation(model.getLocation());
        dto.setLocationName(model.getLocationName());
        if (!forListView) {
            final List<ArrivalCount> acList = model.getArrivalCount();
            final List<ArrivalCountDTO> acDtoList = new ArrayList<>();
            for (final ArrivalCount ac : acList) {
                acDtoList.add(this.arrivalCountConverter.toDTO(ac));
            }
            dto.setArrivalCount(acDtoList);

            if (null != model.getAssetList()) {
                model.getAssetList().stream().map(l -> this.arrivalAssetConverter.toDTO(l))
                        .forEach(dto.getAssetList()::add);
            }
        }
        return dto;
    }
}
